#!/usr/bin/perl

use CGI;
use SvnRep;
use SvnUser;

my $q=CGI->new;
print $q->header(-charset=>'utf-8');
#
# Access control
#
$auth_chk=0;
if(cfg::get('auth_type') eq "syno"){
	$login_name=`/usr/syno/synoman/webman/modules/authenticate.cgi`;
	$login_name=~s/\n//;
	if($login_name ne "admin"){
		$page_login=<<"EOF";
<html>
	<head>
		<style type="text/css">
		body{background-color:#d61f0d;color:#FFFFFF;}
		</style>
	</head>
	<body>
		<h1>Access Denied - <a href="/" target="_block">Login</a></h1>
	</body>
</html>
EOF
		print $page_login;
		exit 0;
	}else{
		$auth_chk=1;
	}
}
if(cfg::get('auth_type') eq "ip"){
	if(grep $_ eq $q->remote_addr(), cfg::allow_ip()){
		$auth_chk=1;
	}
}


if($auth_chk eq 1){
#
#active
#
	$active=$q->url_param('active');
	$rep_name=$q->param('rep_name');
	$rep_context=$q->param('rep_context');
	$rep_anon=$q->param('rep_anon');
	$rep_auth=$q->param('rep_auth');
	$user_uid=$q->param('user_uid');
	$user_pwd=$q->param('user_pwd');
	$user_uid2=$q->param('user_uid2');
	$rm_repo=$q->param('rm_repo');
	$join_repo=$q->param('join_repo');
	if($active eq "create_rep"){
		if(length($rep_name)>0){
			$create=true;
			@exist_rep=SvnRep::get_rep_list();
			foreach $exist_name(@exist_rep){
				if($exist_name eq $rep_name){
					$create=false;
				}
			}
			if($create eq true){
				#print "$rep_name $rep_context $rep_anon $rep_auth<br />";
				SvnRep::create_rep($rep_name);
				SvnRep::set_context($rep_name,$rep_context);
				SvnRep::set_access($rep_name,$rep_anon,$rep_auth);
			}else{
				print "$rep_name exist.";
			}
		}
	}elsif($active eq "set_rep"){
		#print "$rep_name $rep_context $rep_anon $rep_auth<br />";
		SvnRep::set_context($rep_name,$rep_context);
		SvnRep::set_access($rep_name,$rep_anon,$rep_auth);
	}elsif($active eq "del_rep"){
		SvnRep::del_rep($rep_name);
	}elsif($active eq "add_user"){
		#print "$user_uid $user_pwd";
		$r=SvnUser::add_user($user_uid,$user_pwd);
		if($r==0){
			print "User exist.";
		}
	}elsif($active eq "set_user"){
		#print "$user_uid $user_pwd";
		SvnUser::set_pwd($user_uid,$user_pwd);
		SvnRep::set_pwd_allrep($user_uid,$user_pwd);
	}elsif($active eq "del_user"){
		#print "$user_uid"
		SvnRep::del_user_allrep($user_uid);
		SvnUser::del_user($user_uid);
	}elsif($active eq "rm_user_repo"){
		#print "$user_uid2 $rm_repo";
		SvnRep::del_user_in_rep($user_uid2,$rm_repo);
	}elsif($active eq "join_user_repo"){
		#print "$user_uid2 $join_repo";
		$pwd=SvnUser::get_pwd($user_uid2);
		SvnRep::add_user_to_rep($user_uid2,$pwd,$join_repo);
	
	}
#
# General Page
#
$ver_svnadmin=cfg::get('ver_svnadmin');
$ver_svnlook=cfg::get('ver_svnlook');
$page_html=<<"EOF";
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<script type="text/javascript" src="./jqueryui/js/jquery-1.3.2.min.js"></script>
		<script type="text/javascript" src="./jqueryui/js/jquery-ui-1.7.2.custom.min.js"></script>
		<link rel="stylesheet" type="text/css" href="./jqueryui/css/ui-lightness/jquery-ui-1.7.2.custom.css" />
		<link rel="stylesheet" type="text/css" href="./mycss.css" />
		<script type="text/javascript" src="./myjs.js"></script>
		<title>SVNAdmin</title>
	</head>
	<body>
<div id="msgbox">$msg</div>
<div id="main_tab">
	<ul>
		<li><a href="#list">Repository List</a></li>
		<li><a href="#user">User Account</a></li>
		<li><a href="#about">About</a></li>
	</ul>
	<div id="user">
		<h5 style="text-align:right;">
		<a href="#" id="create_user" style="padding:.5em 1em;text-decoration:none" class="ui-state-default ui-corner-all">Create User</a>
		</h5>
		<div id="add_user" style="display:none;">
			<form action="?active=add_user" method="post">
				<label>User Name</label><input type="text" name="user_uid" class="text ui-widget-content ui-corner-all">
				<label>Password</label><input type="password" name="user_pwd" class="text ui-widget-content ui-corner-all">
				<label>Confirm Password</label><input type="password" name="user_pwd2" class="text ui-widget-content ui-corner-all">
			</form>
		</div>
		<div id="set_repo" style="display:none;">
			<select name="all_rep" style="display:none;">
EOF
print $page_html;
@rep_list=SvnRep::get_rep_list();
foreach $rep_name(@rep_list){
	@tmp_access=SvnRep::get_rep_access($rep_name);
	if($tmp_access[1] ne ''){
		printf('<option value="%s">%s</option>',$rep_name,$rep_name);
	}
}
$page_html=<<"EOF";
			</select>
			<form action="?active=set_repo" method="post">
				<label>User Name</label><input type="text" name="user_uid2" disabled="disabled"><br /><br />
				<label>In Repository</label><select name="rm_repo"></select>
				<a href="#" id="bu_rm" class="ui-state-default ui-corner-all" style="padding:0.2em 1em;text-decoration:none;">Remove</a>
				<br /><br />
				<label>Join Repository</label><select name="join_repo"></select>
				<a href="#" id="bu_join" class="ui-state-default ui-corner-all" style="padding:0.2em 1em;text-decoration:none;">Join</a>
				<br />

			</form>
		</div>
		<table id="user_list">
			<tr>
				<th style="width:120px;">User</th>
				<th>Repository</th>
				<th style="width:180px;">Setting</th>
			</tr>
EOF
print $page_html;
@users=SvnUser::get_user_list();
foreach $user_name(@users){
	print "<tr><td>$user_name</td><td>";
	@reps=SvnRep::get_user_in_rep($user_name);
	$count=0;
	foreach $rep(@reps){
		print "$rep";
		if(++$count < scalar(@reps)){
			print ", ";
		}
	}
	print '</td><td style="width:180px;">';
	printf('<a href="#" class="user_set ui-state-default ui-corner-all" style="padding:0.2em 1em;text-decoration:none;">User</a>');
	printf('&nbsp;<a href="#" class="user_repo ui-state-default ui-corner-all" style="padding:0.2em 1em;text-decoration:none;">Repository</a>');
	print "</td></tr>";
	
}

$page_html=<<"EOF";
		</table>
	</div>
	<div id="about">
		<h3>SVNadmin - v0.1</h3>
		<ul>
			<li>Perl - $]</li>
			<li>svnadmin - $ver_svnadmin</li>
			<li>svnlook - $ver_svnlook</li>
		</ul>
		<h3>Feedback</h3>
		<ul>
			<li>nansenat16&#64;null.tw</li>
		</ul>
		<h3>Licenses</h3>
		<ul>
			<li>MIT License</li>
		</ul>
	</div>
EOF
print $page_html;

$list_html=<<"EOF";
<div id="list">
<h5 style="text-align:right;">
<a href="#" id="create_rep" style="padding:.5em 1em;text-decoration:none" class="ui-state-default ui-corner-all">Create Repository</a>
</h5>
<div id="rep_name" style="display:none;">
	<form action="?active=create_rep" method="post">
	<label>Name</label><input type="text" name="rep_name" class="text ui-widget-content ui-corner-all" />
	<label>Context</label><input name="rep_context" class="text ui-widget-content ui-corner-all" />
	<span>Anon-access</span>
	<select name="rep_anon">
		<option value="none">none</option>
		<option value="read">read</option>
		<option value="write">write</option>
	</select><br />
	<span>Auth-access</span>
	<select name="rep_auth">
		<option value="none">none</option>
		<option value="read">read</option>
		<option value="write">write</option>
	</select>
	</form>
</div>
EOF
print $list_html;
$rep_list_html=<<"EOF";
	<table id="rep_list"><tr>
		<th>Name</th>
		<th>Context</th>
		<th>Rev</th>
		<th width="80px">Anon access</th>
		<th width="80px">Auth access</th>
		<th width="80px">Edit</th></tr>
EOF
print $rep_list_html;
	@rep_list=SvnRep::get_rep_list();
	foreach $rep_name (@rep_list){
		@access=SvnRep::get_rep_access($rep_name);
		printf('<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>
			<a href="#" style="padding:0.2em 1em;text-decoration:none" class="rep_edit ui-state-default ui-corner-all">Edit</a></td></tr>',
			$rep_name,
			SvnRep::get_rep_context($rep_name),
			SvnRep::get_rep_rev($rep_name),
			$access[0],$access[1]
		);
	}
	print "</table>";
	print "</div>";

	print "</div>";
}
print "</body></html>";
